
define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";
var wind_async=require('ddf_wind_async');
	
var base=require('component-base');

var history=require('component-history');

var dialog=require('component-dialog');
	
require('component-page');

var upload=require('component-upload');

var shopId=$("#session_shopid").val();	

exports.init=function(m) {

	window.$ddf_merchandise_type = m;

};

exports.resetSearchTypeParams=function(){
	
	
	$("#searchMerchandiseTypeName").val("");
	
	$("#input-belong-type").val("");
		
	searchMerchandiseTypeName="";
};

/**
 * 搜索商品类型
 */
var searchMerchandiseTypeName="";

exports.searchMerchandiseType=function(){
	
	
	searchMerchandiseTypeName=$("#searchMerchandiseTypeName").val();
	
	exports.getMerchandiseTypeList(1,10,shopId,searchMerchandiseTypeName);
	
}


/**
 * 打开商品分类列表
 */
var source="";//1为添加商品的时候，点击自定义分类时进入的  2是从商品分类管理入口进入的
var source_opt="";
exports.openMerchandiseTypeList=function(_source,_source_opt){
	history.addHis(this,arguments);//增加历史
	source=_source;
	source_opt=_source_opt;
	var htmlStr="";
	htmlStr += '<div id="content-header">';
	htmlStr += '<div id="breadcrumb"> <a href=javascript:$ddf_base.load("'+ddf.index.index_js+'") title="返回首页" class="tip-bottom"><i class="icon-home"></i> 首页</a><a href=javascript:$ddf_merchandise.showDiv("content0")>商品管理</a><a href="javascript:void(0)" class="current">商品分类管理</a></div>';
	htmlStr += '</div>    ';
	htmlStr += '<div class="container-fluid">';
	htmlStr += '<div class="row-fluid">';
	htmlStr += '<div class="span12">';
	
	//搜索条件
	htmlStr += '<div class="widget-box">';
	htmlStr += '<div class="widget-title">';
	htmlStr += '<span class="icon">';
	htmlStr += '<i class="icon-search"></i>';
	htmlStr += '</span>';
	htmlStr += '<h5>搜索条件</h5><button type="button" class="label label-success toggle">缩放</button>';
	htmlStr += '</div> ';
	htmlStr += '<div class="widget-content nopadding">';
	htmlStr += '<form class="form-horizontal">';
	htmlStr += '<div class="row-fluid">';
	
	htmlStr += '<div class="span6">';
	htmlStr += '<div class="control-group">';
	htmlStr += '<label class="control-label">商品分类名：</label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<input class="span8 m-wrap" type="text" id="searchMerchandiseTypeName" />';
	htmlStr += '</div>';
	htmlStr += '</div>';
	htmlStr += '</div>';
	
	/*htmlStr += '<div class="span6">';
	htmlStr += '<div class="control-group">';
	htmlStr += '<label class="control-label">分类所属：</label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<input class="span8 m-wrap" id="input-belong-type" type="text" value="" readonly onclick="$ddf_merchandise_type.openBelongTypeDialog()"/> ';
	htmlStr += '</div>';
	htmlStr += '</div>';
	htmlStr += '</div>';*/
	
	htmlStr += '</div>';
	
	htmlStr += '<div class="form-actions">';
	htmlStr += '<button class="btn btn-primary" type="button" onClick="$ddf_merchandise_type.searchMerchandiseType()">搜索</button>';
	htmlStr += ' <button class="btn btn-info" type="button" onClick="$ddf_merchandise_type.resetSearchTypeParams()">重置</button>';
	htmlStr += ' <button class="btn btn-success" type="button" onClick=$ddf_merchandise_type.openAddMerchandiseType("add")>增加</button>';
	htmlStr += ' <button class="btn btn-info" type="button" onClick=$ddf_merchandise_type.back()>返回</button>';
	htmlStr += '</div>';
	
	htmlStr += '</form>';
	htmlStr += '</div>';
	htmlStr += '</div>';
	
	//列表
	htmlStr += '<div class="widget-box">';
	htmlStr += '<div class="widget-title"> <span class="icon"><i class="icon-th"></i></span>';
	htmlStr += '<h5>分类列表</h5>';
	htmlStr += '</div>';
	htmlStr += '<div class="widget-content nopadding dataTables_wrapper">';
	htmlStr += '<table class="table table-bordered data-table">';
	htmlStr += '<thead>';
	htmlStr += '<tr>';
	htmlStr += '<th>序号</th>';
	htmlStr += '<th>商品分类名</th>';
	htmlStr += '<th>上级分类名</th>';
/*	htmlStr += '<th>分类所属</th>';*/
	htmlStr += '<th>操作</th>';
	htmlStr += '</tr>';
	htmlStr += '</thead>';
	htmlStr += '<tbody id="merchandiseTypeList">';
	
	htmlStr += '</tbody>';
	htmlStr += '</table>';
	htmlStr += '<div id="footPageDivType">';
	htmlStr += '</div>';
	htmlStr += '</div>';
	htmlStr += '</div>';
	htmlStr += '</div>';
	htmlStr += '</div>';
	htmlStr += '</div>';
	
	showDiv("content3");
	
	$("#content3").html(htmlStr);
	
	exports.getMerchandiseTypeList(1,10,shopId,searchMerchandiseTypeName);
	
};

exports.back=function(){
	
	if(source_opt=="0"){
		
		showDiv("content1");


	}else if(source_opt=="1"){
		
		showDiv("content2");

	}else{
		
		showDiv("content0");
	}
	
};

/**
 * 获取分类列表
 */
exports.getMerchandiseTypeList=function(pageNumber,pageSize,shopId,merchandiseTypeName){
	
	base.showLoading();
	
	var params={
			"page.pageNumber":pageNumber,
			"page.pageSize":pageSize,
			"shopId":shopId,
			"merchandiseTypeName":searchMerchandiseTypeName
	};
	
	eval(Wind.compile("async",function(){
			
			var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.merchandise.getMerchandiseTypeList,params));
			
			getMerchandiseTypeListCallBack(data);
			
		}))().start();
	
};

/**
 * 获取分类列表_回调函数
 */
function getMerchandiseTypeListCallBack(data){
	
	
	if(data["code"]==1){
		
		var htmlStr="";
		
		for(var i=0;i<data["merchandiseTypeList"].length;i++){
			
			htmlStr += ' <tr>';
			htmlStr += '<td>'+base.getSequence(data["pageNumber"],data["pageSize"],i+1)+'</td>';
			htmlStr += '<td>'+data["merchandiseTypeList"][i]["MERCHANDISE_TYPE_NAME"]+'</td>';
			htmlStr += '<td>'+data["merchandiseTypeList"][i]["PARENT_NAME"]+' </td>';
			/*htmlStr += '<td>'+data["merchandiseTypeList"][i]["SHOP_NAME"]+'</td>';*/
			htmlStr += '<td><a href=javascript:$ddf_merchandise_type.getMerchandiseTypeDetail("'+data["merchandiseTypeList"][i]["MERCHANDISE_TYPE_ID"]+'")>详情</a>';
			htmlStr += '&nbsp;&nbsp;|&nbsp;&nbsp;<a href=javascript:$ddf_merchandise_type.deleteType("'+data["merchandiseTypeList"][i]["MERCHANDISE_TYPE_ID"]+'")>删除</a>';
			htmlStr += '</td></tr> ';
			
			
		}
		
		$("#merchandiseTypeList").html(htmlStr);
					
		$('#footPageDivType').ddf_pagination(data["pageSize"],data["pageNumber"],data["totalCount"],data["totalPage"],{  
			 
			 changePageCallback:function(pageSize,page){  
	        	 
				 exports.getMerchandiseTypeList(page,pageSize,shopId,searchMerchandiseTypeName);
	
			 },
			 changePageSizeCallBack:function(pageSize){
	     	 
				 exports.getMerchandiseTypeList(1,pageSize,shopId,searchMerchandiseTypeName);

	     	 
			 } 
         }); 
	}else{
		
		 
		 base.tips_error(data["message"]);

		
	}

	base.cancelLoading();
}


/**
 * 获取父分类
 */
function getErpMerchandiseTypesList(shopId){

	var params={
			"type":"noparent",
			"shopId":shopId
	};

	eval(Wind.compile("async",function(){
		
		var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.merchandise.getErpMerchandiseTypesList,params));

			getErpMerchandiseParentTypesListCallBack(data);
		
	}))().start();	
	
}

/**
 * 获取父分类_回调函数
 */
function getErpMerchandiseParentTypesListCallBack(data){
	
	
	var htmlStr="",beforeHtmlStr="";
		
	for(var i=0;i<data["erpMerchandiseTypesList"].length;i++){
		if(merchandiseTypeName!=data["erpMerchandiseTypesList"][i]["merchandiseTypeName"]){
			
			if(parentTypeId==data["erpMerchandiseTypesList"][i]["merchandiseTypeId"]){
				beforeHtmlStr = '<li><a href=javascript:$ddf_merchandise_type.setParentTypesName("'+data["erpMerchandiseTypesList"][i]["merchandiseTypeId"]+'","'+data["erpMerchandiseTypesList"][i]["merchandiseTypeName"]+'") >'+data["erpMerchandiseTypesList"][i]["merchandiseTypeName"]+'</a></li>';

				$("#parentTypeName").html(data["erpMerchandiseTypesList"][i]["merchandiseTypeName"]);
				
			}else{
				htmlStr += '<li><a href=javascript:$ddf_merchandise_type.setParentTypesName("'+data["erpMerchandiseTypesList"][i]["merchandiseTypeId"]+'","'+data["erpMerchandiseTypesList"][i]["merchandiseTypeName"]+'") >'+data["erpMerchandiseTypesList"][i]["merchandiseTypeName"]+'</a></li>';

			}
			

		}
		

	}
	
	$("#parentTypesList").html('<li><a href=javascript:$ddf_merchandise_type.setParentTypesName("","请选择") >请选择</a></li>'+beforeHtmlStr+htmlStr);
	//重置全局变量
	merchandiseTypeName = "";
}
/**
 * 设置上级分类
 * @param name
 */
var parentTypeId="";
exports.setParentTypesName=function(id,name){
	
	parentTypeId=id;
	
	$("#parentTypeName").html(name);
	
};
/**
 * 打开添加商品分类
 */

exports.openAddMerchandiseType=function(type){
	history.addHis(this,arguments);//增加历史
	
	var htmlStr="";
	
	htmlStr += '<div id="content-header">';
	htmlStr += '<div id="breadcrumb"> <a href=javascript:$ddf_base.load("'+ddf.index.index_js+'") title="返回首页" class="tip-bottom"><i class="icon-home"></i> 首页</a><a href=javascript:$ddf_merchandise.showDiv("content0")>商品管理</a><a href=javascript:$ddf_merchandise.showDiv("content3")>商品分类管理</a><a href="javascript:void(0)" class="current type_title">添加商品分类</a></div>';
	htmlStr += '</div>    ';

	htmlStr += '<div class="container-fluid">';
	htmlStr += '<div class="row-fluid">';
	htmlStr += '<div class="span12">    	';
	htmlStr += '<div class="widget-box">';
	htmlStr += '<div class="widget-title"> <span class="icon"><i class="icon-th"></i></span>';
	htmlStr += '<h5 class="type_title">添加商品分类</h5>';
	htmlStr += '</div>';
	htmlStr += '<div class="widget-content nopadding">';
	htmlStr += '<div class="form-horizontal">	';
	

/*	htmlStr += '<div class="control-group" id="control-group-shopId">';
	htmlStr += '<label class="control-label">商家：</label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<input class="span9 alert_w"  id="input-mtype-shopName" type="text" value="请选择商家" readonly onclick="$ddf_merchandise_type.openShopDialog()" />   ';
	htmlStr += '</div>';
	htmlStr += '</div>';*/
	

	htmlStr += '<div class="control-group">';
	htmlStr += '<label class="control-label">上级分类：</label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<div class="btn-group">';
	htmlStr += '<button data-toggle="dropdown" class="btn dropdown-toggle" ><span id="parentTypeName">请选择</span>&nbsp;&nbsp;<span class="caret"></span></button>';
	htmlStr += '<ul class="dropdown-menu" id="parentTypesList">';
	htmlStr += '</ul>';
	htmlStr += '</div>';
	htmlStr += '</div>';
	htmlStr += '</div>';
	
	htmlStr += '<div class="control-group" id="control-group-merTypeName">';
	htmlStr += '<label class="control-label">分类名：</label>';
	htmlStr += '<div class="controls">';
	htmlStr += '<input class="span9 alert_w"  id="input-merTypeName" type="text" value="" />   ';
	htmlStr += '</div>';
	htmlStr += '</div>';
	
	
	
	htmlStr += '<div class="form-actions">';
	if("add" == type){
		htmlStr += '<button class="btn btn-primary" type="button" id="type_btn" onclick="$ddf_merchandise_type.addMerchandiseType()" >保存</button>';
		
	}else{
		htmlStr += '<button class="btn btn-primary" type="button" id="type_btn" onclick="$ddf_merchandise_type.updateMerchandiseType()" >保存</button>';
	}
	htmlStr += ' <button class="btn btn-info" type="button" onClick=$ddf_merchandise.showDiv("content3")>返回</button>      ';
	htmlStr += '</div>  ';
	
	htmlStr += '</div>';
	htmlStr += '</div>';
	htmlStr += '</div> ';
	htmlStr += '</div>';
	htmlStr += '</div> ';
	htmlStr += '</div> ';

	showDiv("content4");
	
	$("#content4").html(htmlStr);
	
	if(type=="add"){
		
		getErpMerchandiseTypesList(source);
		
		if(source!=""){
			$("#control-group-shopId").hide();
		}
		
	}

	
}

/**
 * 添加商品分类
 */
exports.addMerchandiseType=function(){
	
	
	var merTypeName=$("#input-merTypeName").val();
	

	if(isNull(merTypeName)){
		
		base.tips_error("商品分类名非法");
		
		return;
	}
	var params={
			"merchandiseTypeName":merTypeName,
			"shopId":shopId,
			"parentId":parentTypeId	
	        };
	
	base.showLoading();

	eval(Wind.compile("async",function(){
			
			var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.merchandise.addMerchandiseType,params));
			
			addMerchandiseTypeCallBack(data);
			
		}))().start();
	
}

/**
 * 添加商品分类_回调函数
 */
function addMerchandiseTypeCallBack(data){
	
	if(data["code"]==1){
		
		if(source==""){
			
			showDiv("content3");

			exports.getMerchandiseTypeList(1,10,shopId,searchMerchandiseTypeName);

		}else{
						
			//添加
			if(source_opt=="0"){
				
				showDiv("content1");


			}
			//修改
			else{
				
				showDiv("content2");

			}
			
			$ddf_merchandise.getErpMerchandiseTypesList(source);
		}
		
		base.tips("添加商品分类成功");



	}else{
		base.tips_error(data["message"])
		
    }

	base.cancelLoading();
}

/**
 * 获取分类详细信息
 */
exports.getMerchandiseTypeDetail=function(merchandiseTypeId){
	history.addHis(this,arguments);//增加历史
	
	exports.openAddMerchandiseType("edit");
	
	$(".type_title").html("修改商品分类");
	
	base.showLoading();
	
	var params={
			"merchandiseTypeId":merchandiseTypeId
	        };
	
	eval(Wind.compile("async",function(){
			
			var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.merchandise.getMerchandiseTypeList,params));
			
			getMerchandiseTypeDetailCallBack(data);
			
		}))().start();
	
}

/**
 * 获取分类详细信息_回调函数
 */
var merchandiseTypeId="",merchandiseTypeName="";

function getMerchandiseTypeDetailCallBack(data){
	

	
	if(data["code"]==1){
		
		shopId=data["merchandiseTypeList"][0]["SHOP_ID"];
				
		getErpMerchandiseTypesList(shopId);
		
		parentTypeId=data["merchandiseTypeList"][0]["PARENT_ID"];
		
		merchandiseTypeId=data["merchandiseTypeList"][0]["MERCHANDISE_TYPE_ID"];
		
		merchandiseTypeName=data["merchandiseTypeList"][0]["MERCHANDISE_TYPE_NAME"];
		
		$("#input-mtype-shopName").val(data["merchandiseTypeList"][0]["SHOP_NAME"]);

		$("#input-merTypeName").val(data["merchandiseTypeList"][0]["MERCHANDISE_TYPE_NAME"]);
		
	
	}else{
		
		 
		 base.tips_error(data["message"]);

		
	}

	base.cancelLoading();
}

/**
 * 修改商品分类
 */
exports.updateMerchandiseType=function(){
	var merTypeName=$("#input-merTypeName").val();
	
	if(isNull(merTypeName)){
		
		base.tips_error("商品分类名非法");
		
		return;
	}
	
	var params={
			"merchandiseTypeName":merTypeName,
			"shopId":shopId,
			"parentId":parentTypeId,
			"merchandiseTypeId":merchandiseTypeId
	        };
	
	base.showLoading();

	eval(Wind.compile("async",function(){
			
			var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.merchandise.updateMerchandiseType,params));
			
			updateMerchandiseTypeCallBack(data);
			
		}))().start();
	
};

/**
 * 修改商品分类_回调函数
 */
function updateMerchandiseTypeCallBack(data){

	if(data["code"]==1){
		
		showDiv("content3")
		
		base.tips("修改商品分类成功");
		
		exports.getMerchandiseTypeList(1,10,shopId,searchMerchandiseTypeName);

	}else{
		base.tips_error(data["message"])
		
    }

	base.cancelLoading();
	
}

//删除分类
exports.deleteType=function(typeId){
	
	var params={
			"merchandiseTypeId":typeId
	        };
	
	base.showLoading();

	eval(Wind.compile("async",function(){
			
			var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.merchandise.deleteMerchandiseType,params));
			
			deleteTypeCallBack(data);
			
		}))().start();
}

/**
 * 修改商品分类_回调函数
 */
function deleteTypeCallBack(data){
	
	if(data["code"]==1){
		
		showDiv("content3");
		
		base.tips("删除商品分类成功");
		
		exports.getMerchandiseTypeList(1,10,shopId,searchMerchandiseTypeName);

	}else{
		base.tips_error(data["message"]);
		
    }

	base.cancelLoading();
	
}


/**
 * 显示DIV
 */
function showDiv(block){
	
	$("#content > div").hide();
	
	if($("#"+block).length<=0){
		
		$("#content").append('<div id="'+block+'"></div>'); 
		
	}else{
		
		$("#"+block).show();
	}
	
	parentTypeId="";
	
}

});